using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace MinoChat.Ui.Behaviors;

/// 自定义装饰器，用于在AdornerLayer上绘制固定大小的滚动条滑块
public class ScrollBarThumbAdorner : Adorner
{
    private readonly Thumb _originalThumb;
    private readonly ScrollBar _scrollBar;
    private const double ThumbHeight = 40;
    private const double ThumbWidth = 7;
    private const double ThumbCornerRadius = 3.5;
    private bool _isMouseOver;

    public ScrollBarThumbAdorner(Thumb thumb, ScrollBar scrollBar) : base(scrollBar)
    {
        _originalThumb = thumb;
        _scrollBar = scrollBar;
        IsHitTestVisible = true; // 装饰器响应鼠标事件
    }

    protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
    {
        // 获取真实滑块的位置
        try
        {
            var thumbPosition = _originalThumb.TransformToAncestor(_scrollBar).Transform(new Point(0, 0));
            var centerY = thumbPosition.Y + (_originalThumb.ActualHeight / 2);

            // 固定40px高的滑块区域
            var thumbRect = new Rect(
                (_scrollBar.ActualWidth - ThumbWidth) / 2,
                centerY - (ThumbHeight / 2),
                ThumbWidth,
                ThumbHeight
            );

            // 确保滑块不超出边界
            if (thumbRect.Y < 0) thumbRect.Y = 0;
            if (thumbRect.Bottom > _scrollBar.ActualHeight)
                thumbRect.Y = _scrollBar.ActualHeight - ThumbHeight;

            // 检查点击是否在可见滑块范围内
            if (thumbRect.Contains(hitTestParameters.HitPoint))
            {
                // 返回真实滑块作为命中目标,让事件传递给它
                return new PointHitTestResult(_originalThumb, hitTestParameters.HitPoint);
            }
        }
        catch { }

        // 返回null让事件穿透到下层
        return null;
    }

    protected override void OnMouseEnter(MouseEventArgs e)
    {
        base.OnMouseEnter(e);
        _isMouseOver = true;
        InvalidateVisual();
    }

    protected override void OnMouseLeave(MouseEventArgs e)
    {
        base.OnMouseLeave(e);
        _isMouseOver = false;
        InvalidateVisual();
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        _originalThumb.RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton)
        {
            RoutedEvent = Mouse.MouseDownEvent,
            Source = _originalThumb
        });
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        _originalThumb.RaiseEvent(new MouseEventArgs(e.MouseDevice, e.Timestamp)
        {
            RoutedEvent = Mouse.MouseMoveEvent,
            Source = _originalThumb
        });
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        _originalThumb.RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton)
        {
            RoutedEvent = Mouse.MouseUpEvent,
            Source = _originalThumb
        });
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);

        // 获取滚动条的透明度，如果透明度为0则不绘制
        if (_scrollBar.Opacity <= 0)
            return;

        // 获取真实滑块的位置
        try
        {
            var thumbPosition = _originalThumb.TransformToAncestor(_scrollBar).Transform(new Point(0, 0));

            // 计算滑块的中心位置
            var centerY = thumbPosition.Y + (_originalThumb.ActualHeight / 2);

            // 固定40px高的滑块，居中对齐
            var thumbRect = new Rect(
                (_scrollBar.ActualWidth - ThumbWidth) / 2,
                centerY - (ThumbHeight / 2),
                ThumbWidth,
                ThumbHeight
            );

            // 确保滑块不超出滚动条边界
            if (thumbRect.Y < 0)
                thumbRect.Y = 0;
            if (thumbRect.Bottom > _scrollBar.ActualHeight)
                thumbRect.Y = _scrollBar.ActualHeight - ThumbHeight;

            // 根据滑块状态选择颜色
            Color baseColor;
            if (_originalThumb.IsDragging)
                baseColor = Color.FromRgb(0xCC, 0xCC, 0xCC);
            else if (_isMouseOver)
                baseColor = Color.FromRgb(0xAA, 0xAA, 0xAA);
            else
                baseColor = Color.FromRgb(0x88, 0x88, 0x88);

            // 应用滚动条的透明度到颜色
            baseColor.A = (byte)(255 * _scrollBar.Opacity);
            var brush = new SolidColorBrush(baseColor);

            // 绘制圆角矩形滑块
            drawingContext.DrawRoundedRectangle(brush, null, thumbRect, ThumbCornerRadius, ThumbCornerRadius);
        }
        catch
        {
            // 如果获取位置失败，不绘制
        }
    }
}